//
//  InventoryController.m
//  HarmonizePub
//
//  Created by rzhy on 16/9/22.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "InventoryController.h"
#import "Config.h"
#import <UIScrollView+EmptyDataSet.h>
#import "Patient.h"
#import "InventoryTableViewCell.h"
#import "WebViewController.h"
#import "ClinicPatmentCell.h"

static NSString * const CellIdentifier = @"Cell";

@interface InventoryController () <UITableViewDataSource, UITableViewDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
{
    Patient *_patient;
    NSArray *_dataArray;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation InventoryController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"每日清单";
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    UINib *nib = [UINib nibWithNibName:[InventoryTableViewCell registerName] bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"ClinicPatmentCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ClinicPatmentCell"];
    _patient = [Patient sharedPatient];
    [self getZymrqdlb];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 住院每日清单列表
- (void)getZymrqdlb {
    NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
    NSDictionary *param = @{@"id" : @(_patient.id)};
    [HTTPSTool POSTRequestWithPath:XHUserZymrqdlb cookie:cookie params:param success:^(NSDictionary * _Nonnull JSON) {
        XHLog(@"住院每日清单列表 -> JSON: %@", JSON);
        _dataArray = JSON[@"data"][@"list"];
        [_tableView reloadData];
    }];
}

#pragma mark - tableView delegate/dataSource Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else{
        return _dataArray.count;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    if (indexPath.section == 0) {
        ClinicPatmentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ClinicPatmentCell"];
        cell.titleLabel.textColor = [UIColor redColor];
        cell.titleLabel.text = @"日期";
        cell.amountLabel.textColor = [UIColor redColor];
        return cell;
    }else{
         InventoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        NSDictionary *dict = _dataArray[indexPath.row];
        cell.timeLabel.textColor = [UIColor blackColor];
        cell.moneyLabel.textColor = [UIColor blackColor];
        cell.timeLabel.text = dict[@"fyrq"];
        cell.moneyLabel.text = dict[@"sum"];
        return cell;
    }
   
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSDictionary *dict = _dataArray[indexPath.row];
    NSString *fyrq = dict[@"fyrq"];
    NSString *zjje = dict[@"sum"];
    NSString *urlString = [NSString stringWithFormat:@"%@?id=%ld&date=%@&amt=%@", XHUserZymrqdmx, (long)_patient.id, fyrq, zjje];
    NSURL *url = [URLTool webURLStringWithPathString:urlString token:[UserDefaultsTool stringForKey:XHToken]];
    WebViewController *webView = [[WebViewController alloc] init];
    webView.title = [fyrq stringByAppendingString:@" 每日清单"];
    webView.url = url;
    [self.navigationController pushViewController:webView animated:YES];
}

#pragma mark - DZNEmptyDataSetSource Method
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"LoadFailure.png"];
    return image;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *string = XHEmptyDes;
    NSAttributedString *abs = [[NSAttributedString alloc] initWithString:string attributes:nil];
    return abs;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -64.0f;
}

#pragma mark - DZNEmptyDataSetDelegate Method
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    [self getZymrqdlb];
}

@end
